<template>
  <div>
    <div class="accountingModel">
      <!-- 标题 -->
      <div class="accountingModel-title">核算模型</div>


      <!-- 条件筛选 -->
      <div class="accountingModel-condition">
        <div class="accountingModel-condition-form">
          <normal-form-plus
            :formModel="formModel"
            :config="config"
            :plus="plus"/>
        </div>
        <div class="accountingModel-condition-btn">
          <el-button>查询</el-button>
          <el-button>重置</el-button>
          <el-button @click="add()">新增</el-button>
        </div>
      </div>


      <!-- 表格 -->
      <div class="accountingModel-report">
        <normal-table
          :tableData="tableData"
          :paginationConfig="paginationConfig"
          :tableLabel="tableLabel"
          :operationConfig="operationConfig"
          :operationList="operationList"
          @event="handleEvent"
          @changePage="changePage"></normal-table>
      </div>

      <!-- 新增核算模型-抽屉 -->
      <el-drawer
        title="新增核算模型"
        :visible.sync="drawer"
        :direction="'rtl'">
        <normal-form
          :formModel="addFormModel"
          :config="addConfig"
          :plus="plus"/>
        <div class="drawer-btn">
          <el-button type="primary" @click="createModel()">确认</el-button>
        </div>

      </el-drawer>
    </div>

    <div class="test">
      <d3graph/>
    </div>
  </div>
</template>

<script>

import NormalForm from '@/components/NormalForm'
import NormalTable from '@/components/NormalTable'
import NormalFormPlus from '@/components/NormalFormPlus'

export default {
  name: 'accountingModel',
  components: {
    NormalTable,
    NormalFormPlus,
    NormalForm
  },
  data() {
    return {
      // 新增碳足迹报告-数据
      addFormModel: {
        lifecycle: "",//	生命周期：0-从摇篮到大门，1-从摇篮到坟墓		true
        name: "",//	模型名称
        note: "",//	说明
        productId: "",//	产品id
      },
      addConfig: [
        {
          id: '1',
          type: 'input',
          model: 'name',
          name: '模型名称',

          edit: true,
          props: {
            placeholder: '模型名称',
            width: 200,
          }
        },
        {
          id: '2',
          type: 'select',
          model: 'lifecycle',
          name: '生命周期',
          edit: true,
          props: {
            placeholder: '生命周期',
            options: [
              {value: "0", label: "从摇篮到大门"},
              {value: "1", label: "从摇篮到坟墓"},
            ]
          }
        },
        {
          id: '3',
          type: 'select',
          model: 'productId',
          name: '产品名称',
          edit: true,
          props: {
            placeholder: '产品名称',
            options: [{value: "0", label: "从摇篮到大门"},
              {value: "1", label: "从摇篮到坟墓"},]
          }
        },
        {
          id: '4',
          type: 'text',
          model: 'note',
          name: '说明',
          edit: true,
          props: {
            placeholder: '说明',
          }
        },

      ],
      temAddConfig: [
        {
          id: '1',
          type: 'input',
          model: 'name',
          name: '模型名称',

          edit: true,
          props: {
            placeholder: '模型名称',
            width: 200,
          }
        },
        {
          id: '2',
          type: 'select',
          model: 'lifecycle',
          name: '生命周期',
          edit: true,
          props: {
            placeholder: '生命周期',
            options: [
              {value: "0", label: "从摇篮到大门"},
              {value: "1", label: "从摇篮到坟墓"},
            ]
          }
        },
        {
          id: '3',
          type: 'select',
          model: 'productId',
          name: '产品名称',
          edit: true,
          props: {
            placeholder: '产品名称',
            options: [{value: "0", label: "从摇篮到大门"},
              {value: "1", label: "从摇篮到坟墓"},]
          }
        },
        {
          id: '4',
          type: 'text',
          model: 'note',
          name: '说明',
          edit: true,
          props: {
            placeholder: '说明',
          }
        },

      ],
      plus: {},
      drawer: false,
      formModel: {},
      config: [
        {
          id: 1,
          items: [
            {
              id: '1',
              edit: true,
              type: 'input',
              span: 6,
              model: 'modelLevel',
              name: '',
              props: {
                placeholder: "模型名称",

              }
            },
            {
              id: '2',
              edit: true,
              type: 'input',
              span: 6,
              model: 'modelLevel',
              name: '',
              props: {
                placeholder: "模型来源",
                options: [
                  {lable: '1', value: '1'},
                  {lable: '2', value: '2'},
                ]

              }
            },

          ]
        },
      ],
      plus: {},
      tableData: [],
      paginationConfig: {
        total: 100,
        page: 1,
        pageSize: 10,
        show: true
      },
      operationList: [
        {
          name: '编辑',
          order: 1,
          buttonClass: 'primary',
          plus: {}
        },
        {
          name: '删除',
          order: 2,
          buttonClass: 'danger',
          plus: {}
        }
      ],
      operationConfig: {select: false},
      tableLabel: [
        {
          prop: "order",
          label: "序号",
          width: 70
        },
        {
          prop: "name",
          label: "模型名称",
          width: 200
        },
        {
          prop: "note",
          label: "模型描述",
        },
        {
          prop: "lifecycle",
          label: "生命周期",
          codeTable: {
            "true": '从摇篮到大门',
            "false": '从摇篮到坟墓'
          },
          width: 200
        },
      ],

    }
  },
  mounted() {
    this.getAllProduct()
    this.getModelByPage()
  },
  methods: {
    add() {
      this.drawer = true
    },
    // 获取所有模型列表
    getAllProduct() {
      this.addConfig = []
      this.temAddConfig[2].props.options = []
      this.$API.product.getProductByPage({
        pageNo: 1,
        pageSize: 100,
      }).then(response => {
        var data = response.data.list
        var li = []
        for (var i = 0; i < data.length; i++) {
          li.push({
            label: data[i].name,
            value: data[i].id,
          })
        }
        this.temAddConfig[2].props.options = li
        this.addConfig = this.temAddConfig
      })
    },
    // 创建核算模型
    createModel() {
      this.drawer = false
      this.addFormModel.lifecycle = this.addFormModel.lifecycle == '0' ? false : true

      this.$API.product.createCalculationModel({
        ...this.addFormModel
      }).then(response => {
        this.getModelByPage()
        // this.$router.push("/carbonFootprintVerification/productCarbonAccounting/addProductCarbonAccounting")
      })
    },
    // 获取核算模型
    getModelByPage() {
      if (this.$MOCK.mock) {
        this.tableData = this.$MOCK.product.modelList.data.list
        this.paginationConfig.total = this.$MOCK.product.modelList.data.total
        return
      }
      this.$API.product.getModelByPage({
        pageNo: this.paginationConfig.page,
        pageSize: this.paginationConfig.pageSize,
      }).then(response => {
        this.tableData = response.data.list
        this.paginationConfig.total = response.data.total
      })

    },
    turnTo() {
      this.drawer = false
      this.$router.push("/carbonFootprintVerification/productCarbonAccounting/addProductCarbonAccounting")
    },
    // 处理表格点击事件
    handleEvent(order, row) {
      if (order === 1) {
        console.log(row)
        this.$router.push({
          path: "/carbonFootprintVerification/productCarbonAccounting/addProductCarbonAccounting",
          query: {id: row.id}
        })
      }
    },
    // 分页点击事件
    changePage() {
      this.getModelByPage()
    }
  }
}
</script>

<style lang="less" scoped>
.drawer-btn {
  margin-left: 150px;
}

.accountingModel {

  height: 1680px;
  overflow: hidden;
  padding: 15px 10px;
  // 标题
  .accountingModel-title {
    font-size: 16px;
    font-weight: bold;
  }

  .accountingModel-condition {
    display: flex;

    .accountingModel-condition-form {
      flex: 3;
    }

    .accountingModel-condition-btn {
      flex: 1;
    }
  }

  // 核算报告表格
  .accountingModel-report {
    .accountingModel-history-title {
      font-size: 14px;
    }
  }
}

.test {
  height: 50%;
  position: relative;
}
</style>
